var taskTable = function () {
    var initPickers = function () {
        //init date pickers
        $('div.date-picker').datepicker({
            language: 'zh-CN',
            rtl: App.isRTL(),
            weekStart: 1,
            clearBtn: true,
            autoclose: true,
            todayHighlight: 1
        });
    };

    var reloadTable = function(pageFlag){
        $("#datatable_ajax").DataTable().draw(pageFlag)
    };
    var onSortColumn = function(sortColumn, sortDirection) {
        switch (sortColumn) {
            case "taskName":
                sortColumn = "task_name";
                break;
            case "taskLevel":
                sortColumn = "task_level";
                break;
            case "taskClassify":
                sortColumn = "task_classify";
                break;
            case "taskStyle":
                sortColumn = "task_style";
                break;
            case "taskHours":
                sortColumn = "task_hours";
                break;
            case "showBiddingDeadLine":
                sortColumn = "task_deadline";
                break;
            case "showTaskDeadline":
                sortColumn = "bidding_deadline";
                break;
            case "authorId":
                sortColumn = "author_id";
                break;
        }
        return onSortColumnDefault(sortColumn, sortDirection);
    };

    /**
     * dataTable事件初始化方法
     */
    var handleRecords = function() {
        var grid = new DataTable();
        var $table = $("#datatable_ajax");
        grid.init({
            src: $table,
            url:basePath +"/task/listAllTask",
            onSortColumn:onSortColumn,
            onQuery: function (data) {
                data.taskName = $("#taskName").val();
                data.taskClassify = $("#taskClassify").val();
                data.taskStyle = $("#taskStyle").val();
                data.taskTool = $("#taskTool").val();
                data.taskHours = $("#taskHours").val();
                data.taskLevel = $("#taskLevel").val();
                data.startCreateTime = $("#startCreateTime").val();
                data.endCreateTime = $("#endCreateTime").val();
                data.startBiddingDeadLine = $("#startBiddingDeadLine").val();
                data.endBiddingDeadLine = $("#endBiddingDeadLine").val();
                data.startTaskDeadline = $("#startTaskDeadline").val();
                data.endTaskDeadline = $("#endTaskDeadline").val();
                data.authorId = $("#authorId").val();
                data.partRelease = $("#partRelease").val();
                data.taskDescription = $("#taskDescription").val();
            },
            dataTable: {
                "columns": [
                    {data: 'taskName', orderable: true},
                    {data: 'taskLevel', orderable: false},
                    {data: 'taskClassify', orderable: false},
                    {data: 'taskStyle' , orderable: false},
                    {data: 'taskTool' , orderable: false},
                    {data: 'taskHours' , orderable: false},
                    {data: 'partRelease' , orderable: false},
                    {data: 'showCreateTime', orderable: false },
                    {data: 'showBiddingDeadLine', orderable: true,render:function(data){return data.substring(0,19)}},
                    {data: 'showTaskDeadline', orderable: true ,render:function(data){return data.substring(0,19)}},
                    {data: 'authorId', orderable: false },
                    {data: 'taskDescription', orderable: false, render: function (data) {
                        return data.length > 10 ? "<span title='" + data + "'>" + data.substring(0, 9) + "</span>" : data
                    } },
                    {
                        data: 'operate', orderable: false ,
                        render: function (data, type, full) {
                                var btnStr = '<i class="fa fa-eye faa-flash animated-hover"></i>查看附件';

                                return '<a class="edit btn default btn-xs purple" onclick="taskTable.displayAttachment(\'' + full.id + '\')">'+btnStr+'</a>';

                        }
                    }
                ]
            }
        });

        var $tableWrapper = $("#datatable_ajax_wrapper");
        $tableWrapper.find(".dataTables_filter input").addClass("form-control input-medium input-inline"); // modify table search input
        $tableWrapper.find(".dataTables_length select").addClass("form-control input-small"); // modify table per page dropdown

        //重新载入按钮事件
        $("#reloadTable").click(function(){
            reloadTable(false);
        });

        //显示列选择按钮事件
        $('#datatable_ajax_column_toggler').find('input[type="checkbox"]').change(function(){
            var iCol = parseInt($(this).attr("data-column"));
            $table.DataTable().column(iCol).visible(!$table.DataTable().column(iCol).visible());
        });
        $("#exportExcel").click(function(){
            formSub(basePath + "/task/exportExcelAllTask")
        })
    };

    return {
        init: function () {
            handleRecords();
            initPickers();
        },
        displayAttachment:function(id){
            $("#taskDetailIframe").attr("src",basePath + "item/listAllAttachmentDo?nodeId=" + id);
            $("#modalDialog").modal("show");
        },
        reloadTable: reloadTable
    };

}();

